// pages/favorite/favorite.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    //tabbar
    tabbar: {},
    isIphoneX: app.globalData.isIphoneX,
    showAuth: false,
    
    themeColor: '',
    list: [],
    keyword: '',
    currentIndex: 0,
    page: 1,
    size: 0,
    total: 0
  },
  onLoad: function () {
    //隐藏系统tabbar
    wx.hideTabBar();
    //tabbar调用
    app.editTabbar();
    
    self = this;
    self.getStyle();
  },
  onShow: function () {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          self.init();
        } else {
          wx.setStorageSync("path", self.route);
          // wx.reLaunch({
          //   url: '/pages/login/login'
          // })
          self.setData({
            showAuth: true
          })
        }
      }
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    // var url = host + '/yq/item/subscribes';
    var url = host + '/yq/order/cards';
    var data = {
      keyword: self.data.keyword
    };
    var success = function (res) {
      console.log(res);
      self.setData({
        list: res.info,
        page: res.page,
        size: res.size,
        total: res.total
      })
    };
    var fail = function (res) {};
    var complete = function (res) {};
    app.request(url, data, success, fail, complete);
  },
  onKeywordChange(e) {
    self.setData({
      keyword: e.detail.value
    })
  },
  search(e) {
    self.init();
  },
  expand(e) {
    var index = e.currentTarget.dataset.index;
    var itemId = e.currentTarget.dataset.itemid;
    if (Number(index) == self.data.currentIndex) {
      wx.navigateTo({
        url: '/pages/productInfo/productInfo?itemId=' + itemId
      })
      return;
    }
    self.setData({
      currentIndex: Number(index)
    })
  },
  invite(e) {
    var productId = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/pintuan/pintuan?productId=' + productId
    })
  },
  onShareAppMessage(object) {
    console.log(object);
    if (object.from == "button") {
      var itemId = object.target.dataset.itemid;
      var productId = object.target.dataset.productid;
      var groupId = object.target.dataset.groupid;
      var shareTitle = object.target.dataset.sharetitle;
      var shareImage = object.target.dataset.shareimage;
      return {
        title: shareTitle,
        imageUrl: shareImage,
        path: "/pages/productInfo/productInfo?itemId=" + itemId + "&productId=" + productId + "&groupId=" + groupId
      }
    } else {
      return null;
    }
  },
  showQRCode(e) {
    var url = e.currentTarget.dataset.url;
    if (!url) {
      return;
    }
    wx.previewImage({
      urls: [url]
    })
  },
  none(e) {
    return;
  }
})
